<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                           |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\Account\Back\Employee;

/**
 * 预约看车管理
 *
 * @author Xmh
 */
class Reserve {

    private $ReserveModel;

    public function __construct() {
        $this->ReserveModel = M("reserve");
    }

    /**
     * 获得定制购车列表
     */
    public function reserve_list($cols = '*') {
        return $this->ReserveModel->field($cols)->select();
    }

    public function get_reserve_list($page, $size, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $size = (int) $size < 1 ? 10 : (int) $size;
        $map = array();
        /**
         * 条件查询
         * 1、登记ID 2、姓名 3、手机号 4、车辆编号 4、车型 5、渠道 6、开始时间、结束时间 
         */
        if ($params['id']) {
            $map['re_id'] = array('eq', $params['id']);
        }
        if ($params['status']) {
            $map['status'] = array('eq', $params['status']);
        }
        if ($params['name']) {
            $map['name'] = array('like', '%' . $params['name'] . '%');
        }
        if ($params['phone']) {
            $map['phone'] = array('like', '%' . $params['phone'] . '%');
        }
        if ($params['car_no']) {
            $map['car_no'] = array('eq', $params['car_no']);
        }
        if ($params['car_type']) {
            $map['car_type'] = array('eq', $params['car_type']);
        }
        if ($params['comefrom']) {
            $map['comefrom'] = array('eq', $params['comefrom']);
        }
        if ($params['emp']) {
            $map['emp_id'] = array('eq', $params['emp']);
        }
        if ($params['start_time']) {
            $map['create_at'] = array('egt', $params['start_time'] . ' 00:00:00');
        }
        if ($params['end_time']) {
            $map['create_at'] = array('elt', $params['end_time'] . ' 23:59:59');
        }
        if ($params['start_time'] && $params['end_time']) {
            $map['create_at'] = array(array('egt', $params['start_time'] . ' 00:00:00'), array('elt', $params['end_time'] . ' 23:59:59'));
        }
        $cols = "*";
        $order = "re_id DESC";
        $total = $this->ReserveModel->where($map)->count();
        $list = $this->ReserveModel->alias('cr')
                ->field($cols)
                ->where($map)
                ->limit($size)
                ->page($page)
                ->order($order)
                ->select();
//        echo M()->getLastSql();
        $list_info = array();
        foreach ($list as $k => $v) {
            $list_info[$k]['re_id'] = $v['re_id'];
            $emp = Employee::user_info_id($v['emp_id']);
            $list_info[$k]['emp'] = $emp['real_name'];
            $list_info[$k]['name'] = $v['name'];
            $list_info[$k]['phone'] = $v['phone'];
            $list_info[$k]['car_id'] = $v['car_id'];
            $list_info[$k]['comefrom'] = $v['comefrom'];
            $list_info[$k]['status'] = $v['status'];
            $list_info[$k]['reserve_time'] = $v['reserve_time'];
            $list_info[$k]['createtime'] = $v['createtime'];
        }
        $result = array(
            'list' => $list_info,
            'total' => $total
        );
        return $result;
    }

    /**
     * 提交定制信息
     */
    public function reserve_add($data) {
        return $this->ReserveModel->add($data);
    }

    /**
     * 获取定制信息详情
     * @param int $re_id
     */
    public function get_reserve_info($re_id) {
        $condition['re_id'] = $re_id;
        $list = $this->ReserveModel->where($condition)->find();
        return $list;
    }

    /**
     * 处理定制信息
     */
    public function update_reserve_post($data) {
        $data['reserve_time'] = date("Y-m-d H:i:s", time());
        return $this->ReserveModel->save($data);
    }

}
